Skip to content

Conversation

kcrommett
Copy link

@kcrommett kcrommett commented Oct 12, 2025

Summary

  • Refactor File.Content to a discriminated union for type-safe text vs binary
    handling
  • Add MIME detection and include it in binary responses; update SDK types and do
    cs

Changes

  • Replace File.Content with a discriminated union keyed by type:
    • text: { type: "text", content, diff?, patch? }
    • binary: { type: "binary", content /* base64 */, mimeType }
  • Add getMimeType(path) with common extension map and application/octet-strea m fallback
  • Add isBinaryFile(path, file) using extension heuristics plus a null-byte sca
    n of the first up to 512 bytes
  • Update read(path):
    • Return binary with base64 content and mimeType for detected binaries
    • Return text content; when tracked in git, compute diff and full patch
      (tries unstaged, then staged)
  • Update SDK generated types to expose FileContent as a discriminated union
  • Update SDK docs with usage examples for binary (data URL) vs text content

Benefits

  • Strong compile-time guarantees when handling text vs binary content across ser
    ver and SDK
  • Explicit mimeType metadata enables correct client-side rendering and process
    ing
  • Clear API: diff/patch only on text variant; binary is base64 with metadata
  • Simpler client logic using content.type discriminator

@kcrommett kcrommett marked this pull request as ready for review October 12, 2025 05:14
@rekram1-node rekram1-node self-requested a review October 12, 2025 05:30
@rekram1-node
Copy link
Collaborator

@kmk142789 please stop reviewing prs, the review doesnt do anything since you arent a maintainer and it can lead to confusion with those trying to contribute

@rekram1-node
Copy link
Collaborator

@kcrommett ill peep this today thanks!

Comment on lines 80 to 100
const mimeTypes: Record<string, string> = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".bmp": "image/bmp",
".webp": "image/webp",
".ico": "image/x-icon",
".svg": "image/svg+xml",
".zip": "application/zip",
".tar": "application/x-tar",
".gz": "application/gzip",
".pdf": "application/pdf",
".wasm": "application/wasm",
".exe": "application/x-msdownload",
".dll": "application/x-msdownload",
".so": "application/x-sharedlib",
}
return mimeTypes[ext] || "application/octet-stream"
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the bun api return the mime type automatically?

Comment on lines 257 to 260
const content = Buffer.from(buffer).toString("base64")
return { type: "binary", content, mimeType: getMimeType(full) }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For images would it be best to base64 encode them here? Should we return raw bytes?

Idk maybe we should take some inspiration from S3 on this one...

Whats the most common?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sticking with base64. We’re shipping file content inside a JSON payload so it needs to be text-safe, and base64 is the de facto approach (AWS S3’s JSON APIs do the same). Returning raw bytes would force a binary response instead of the existing schema, so we’d lose compatibility with the current endpoint. With the new encoding: "base64" flag, clients can reliably decode via Buffer.from(content, "base64") or similar.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet ill test your latest changes and then this should be good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants